<script setup lang="ts">
import { eosChainId } from '@/common/util/network';

const showExportWallet = ref(false);
const showDestroyWallet = ref(false);
const showChangePassword = ref(false);
const showViewPrivateKey = ref(false);
const showMnemonicWords = ref(false);
const showMnemonicModal = ref(false)

// 账号管理
const router = useRouter();
const handleAccountManage = () => {
    // 直接跳转当前账号公链的账号列表
    let currentChainId = useChainStore().currentChainId;
    if (!currentChainId) currentChainId = eosChainId;

    router.push({
        name: 'account-manage',
        query: { chainId: currentChainId },
    });
};

onMounted(()=>{
  const mne = localStorage.getItem('mnemonic')
  if (mne){
    showMnemonicModal.value = true
  }else{
    showMnemonicModal.value = false
  }
})

// 清除缓存
const { t } = useI18n();
const handleClearCache = async () => {
    await localCache.remove('cachedAbis');
    window.msg.success(t('public.executeSuccess'));
};

const handleLockWallets = async () => {
    useUserStore().setLocked();
}
</script>

<template>
    <div class="full-router">
        <div class="full-inner">
            <page-header :title="$t('setting.manageWallets')"></page-header>

            <div class="cover-content _effect">
                <n-scrollbar class="full">
                    <div class="setting-group">
                        <!-- account manager -->
                        <div @click="handleAccountManage" class="setting-item">
                            <div class="setting-item-left">
                                <div class="item-title">{{ $t('setting.managePermissions') }}</div>
                            </div>
                            <div class="setting-item-right">
                                <img class="bg-img" src="@/assets/images/right_arrow@2x.png" />
                            </div>
                        </div>

                        <!-- export wallet -->
                        <div @click="showExportWallet = true" class="setting-item">
                            <div class="setting-item-left">
                                <div class="item-title">{{ $t('setting.exportWallet') }}</div>
                            </div>
                            <div class="setting-item-right">
                                <img class="bg-img" src="@/assets/images/right_arrow@2x.png" />
                            </div>
                        </div>
                        <!--View private key-->
                        <div @click="showViewPrivateKey = true" class="setting-item">
                          <div class="setting-item-left">
                            <div class="item-title">{{ $t('setting.viewPrivateKey') }}</div>
                          </div>
                          <div class="setting-item-right">
                            <img class="bg-img" src="@/assets/images/right_arrow@2x.png" />
                          </div>
                        </div>
                        <!--View mnemonic words-->
                        <div @click="showMnemonicWords = true" class="setting-item" v-show="showMnemonicModal">
                          <div class="setting-item-left">
                            <div class="item-title">{{ $t('setting.viewMnemonicWords') }}</div>
                          </div>
                          <div class="setting-item-right">
                            <img class="bg-img" src="@/assets/images/right_arrow@2x.png" />
                          </div>
                        </div>
                        <!-- destroy wallet -->
                        <div @click="showDestroyWallet = true" class="setting-item">
                            <div class="setting-item-left">
                                <div class="item-title">{{ $t('setting.destroyWallet') }}</div>
                            </div>
                            <div class="setting-item-right">
                                <img class="bg-img" src="@/assets/images/right_arrow@2x.png" />
                            </div>
                        </div>

                        <!-- change password -->
                        <div @click="showChangePassword = true" class="setting-item">
                            <div class="setting-item-left">
                                <div class="item-title">{{ $t('setting.changePassword') }}</div>
                            </div>
                            <div class="setting-item-right">
                                <img class="bg-img" src="@/assets/images/right_arrow@2x.png" />
                            </div>
                        </div>

                        <!-- clear cache -->
                        <div @click="handleClearCache" class="setting-item">
                            <div class="setting-item-left">
                                <div class="item-title">{{ $t('setting.clearAbiCache') }}</div>
                            </div>
                            <div class="setting-item-right">
                                <img class="bg-img" src="@/assets/images/right_arrow@2x.png" />
                            </div>
                        </div>

                        <!-- clear cache -->
                        <div @click="handleLockWallets" class="setting-item">
                            <div class="setting-item-left">
                                <div class="item-title">{{ $t('setting.lockWallets') }}</div>
                            </div>
                            <div class="setting-item-right">
                                <img class="bg-img" src="@/assets/images/right_arrow@2x.png" />
                            </div>
                        </div>

                    </div>
                </n-scrollbar>
            </div>

            <!-- export wallet -->
            <export-wallet
                :is-show="showExportWallet"
                @close="showExportWallet = false"
            ></export-wallet>

            <!-- destroy wallet -->
            <destroy-wallet
                :is-show="showDestroyWallet"
                @close="showDestroyWallet = false"
            ></destroy-wallet>

            <!-- change password -->
            <change-password
                :is-show="showChangePassword"
                @close="showChangePassword = false"
            ></change-password>
            <!--view Private Key-->
            <view-private-key
                :is-show="showViewPrivateKey"
                @close="showViewPrivateKey = false"
            >
            </view-private-key>
          <view-mnemonic-words
              :is-show="showMnemonicWords"
              @close="showMnemonicWords = false"
          >
          </view-mnemonic-words>
        </div>
    </div>
</template>

<style lang="scss" scoped>
.setting-group {
    padding: 0;
    background-color: rgba(0,0,0,0);

    .setting-item {
        // height: 60px;
        // border-bottom: 1px solid $color-separate;
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        align-items: center;
        cursor: pointer;
        // padding-left: 15px;
        // padding-right: 15px;
        padding: 0.75rem 0.75rem;
        margin: 0.25rem 0;

        .setting-item-left {
            display: flex;
            flex-direction: row;
            justify-content: flex-start;
            align-items: center;
        }
        .setting-item-right {
            display: flex;
            flex-direction: row;
            justify-content: flex-end;
            align-items: center;
            img {
                width: 7.5px;
                height: auto;
            }
        }
        .item-title {
            margin-right: auto;
            font-size: 16px;
            line-height: 24px;
            font-weight: 500;
            color: hsl(212.7deg 8.45% 57%);
            justify-self: flex-start;
        }
        .item-subtitle {
            font-size: 12px;
            color: #cccccc;
        }
    }
}
</style>